/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.service;

import com.aaron.platform.dao.IProjectDao;
import com.aaron.platform.entity.FgysProject;
import com.aaron.platform.exceptions.ProjectActiveException;
import com.aaron.platform.exceptions.ProjectAllreadyExistException;
import com.aaron.platform.exceptions.ProjectNotExistException;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;

/**
 * implementation of IProjectMgr
 * @author chenzhuohua
 */
public class ProjectMgrImp implements IProjectMgr {

    private IProjectDao projectDao;

    public void setProjectDao(IProjectDao projectDao) {
        this.projectDao = projectDao;
    }

    public FgysProject findProjectById(Integer projectId) {
        return projectDao.findById(projectId);
    }

    public FgysProject findProjectByCode(String projectCode) {
        return projectDao.findProjectByCode(projectCode);
    }

    public List<FgysProject> findProjectByName(String projectName) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(FgysProject.class);
        detachedCriteria = detachedCriteria.add(Expression.ilike(FgysProject.PROPERTY_PROJECT_NAME,
                        projectName, MatchMode.ANYWHERE));
        return projectDao.findByCriteria(detachedCriteria);
    }

    public void addProject(FgysProject fgysProject) throws ProjectAllreadyExistException {
        // 名称和编号不允许重复
        if (findProjectByName(fgysProject.getProjectName()) != null || findProjectByCode(fgysProject.getProjectCode()) != null) {
            throw new ProjectAllreadyExistException();
        }
        projectDao.save(fgysProject);
    }

    public void updateProject(FgysProject fgysProject) throws ProjectNotExistException, ProjectAllreadyExistException {
        // 若项目被删除，抛出异常
        if (findProjectById(fgysProject.getProjectId()) == null) {
            throw new ProjectNotExistException();
        }
        // 若更新的名称和其他项目重复，抛出异常
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(FgysProject.class);
        detachedCriteria = detachedCriteria.add(Expression.ne(FgysProject.PROPERTY_PROJECT_ID, fgysProject.getProjectId()))
                .add(Expression.eq(FgysProject.PROPERTY_PROJECT_NAME, fgysProject.getProjectName()));
        if (projectDao.findByCriteria(detachedCriteria) != null) {
            throw new ProjectAllreadyExistException();
        }
        projectDao.update(fgysProject);
    }

    public void removeProject(Integer projectId) throws ProjectNotExistException, ProjectActiveException {
        FgysProject fgysProject;
        // 若项目被删除，抛出异常
        fgysProject = findProjectById(projectId);
        if (fgysProject == null) {
            throw new ProjectNotExistException();
        }
        
        // 若项目处于激活状态，不允许删除
        // 判断逻辑站点是否属于该项目 此处调用根据项目ID查询所有站点（代码暂缺）

        // 删除项目
        projectDao.remove(fgysProject);
        
    }

}
